package com.bilin.coin.network.callback;

import com.bilin.coin.utils.LogUtlis;

import org.json.JSONArray;
import org.json.JSONException;


import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

/**
 * Created by LG on 2018/5/1.
 */

public abstract class WebSocketCallBack extends WebSocketListener {

    //发送消息
    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        super.onOpen(webSocket, response);
        onCallBackOpen(webSocket, response);
    }

    //得到消息
    @Override
    public void onMessage(final WebSocket webSocket, final String text) {
        super.onMessage(webSocket, text);
        LogUtlis.e("----上游消息----"+text);
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Exception {
                //LogUtlis.e("上游发送消息"+text);
                emitter.onNext(text);
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        onRxSubscribe(d);
                        //d.dispose();
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        //LogUtlis.e("中接受到数据" + s);
                        int index = s.indexOf(",[");
                        if (index != -1) {
                            String message = s.substring(index + 1);
                            //if (message.indexOf("ok")==-1) {
                                try {
                                    //LogUtlis.e("中游解析数据"+message);
                                    JSONArray jsonName = new JSONArray(message);
                                    //LogUtlis.e("key---" + jsonName.get(0) + " ---value--" + jsonName.get(1));
                                    String key = (String) jsonName.get(0);
                                    String value="";
                                    switch (key){
                                        case "entrust-buy":
                                            value = (String) jsonName.get(1);
                                            onCallBackMessage(webSocket, key, value);
                                            break;
                                        case "entrust-sell":
                                             value = (String) jsonName.get(1);
                                            onCallBackMessage(webSocket, key, value);
                                            break;
                                        case "real":
                                            value = (String) jsonName.get(1);
                                            onCallBackMessage(webSocket, key, value);
                                            break;
                                        case "entrust-update":
                                            value = (String) jsonName.get(1);
                                            onCallBackMessage(webSocket, key, value);
                                            break;
                                        case "notification":

                                            value = (String) jsonName.get(1);
                                            onCallBackMessage(webSocket, key, value);
                                            break;
                                        case "price":
                                            JSONArray  jsonArray = (JSONArray ) jsonName.get(1);

                                            value = jsonArray.toString();
                                            onCallBackMessage(webSocket, key, value);
                                            break;
                                    }


                                } catch (Exception e) {
                                    //LogUtlis.e("中游异常"+e.toString());
                                    onRxError(new Throwable(e));
                                    e.printStackTrace();
                                }
                            //}
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //LogUtlis.e("中游异常"+e.toString());
                        onRxError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //关闭连接中
    @Override
    public void onClosing(WebSocket webSocket, int code, String reason) {
        super.onClosing(webSocket, code, reason);
        onCallBackClosing(webSocket, code, reason);
    }

    //关闭连接
    @Override
    public void onClosed(WebSocket webSocket, int code, String reason) {
        super.onClosed(webSocket, code, reason);
        onCallBackClosing(webSocket, code, reason);
    }

    //抛异常会进入其中
    @Override
    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
        super.onFailure(webSocket, t, response);
        onCallBackFailure(webSocket, t, response);
    }

    //连接成功
    public abstract void onCallBackOpen(WebSocket webSocket, Response response);

    //连接成功后接受到消息
    public abstract void onCallBackMessage(WebSocket webSocket, String key, String value) throws JSONException;

    //关闭消息回调
    public abstract void onCallBackClosing(WebSocket webSocket, int code, String reason);

    // 抛异常回调
    public abstract void onCallBackFailure(WebSocket webSocket, Throwable t, Response response);

    //取消rxjava
    public abstract void onRxSubscribe(Disposable d);

    public abstract void onRxError(Throwable e);

}
